package com.yingxin.yxerp.module.report.task.controller;

import com.yingxin.yx.framework.commons.enums.task.DynamicTaskLogCategoryEnum;
import com.yingxin.yx.framework.commons.enums.task.DynamicTaskName;
import com.yingxin.yx.framework.commons.enums.task.DynamicTaskTypeEnums;
import com.yingxin.yx.framework.commons.vo.KeyValueRespVo;
import com.yingxin.yxerp.module.report.entity.enums.DynamicTaskLogStatusEnums;
import com.yingxin.yxerp.module.report.task.entity.DynamicTask;
import com.yingxin.yxerp.module.report.task.service.IDynamicTaskService;
import com.yingxin.yxerp.module.report.task.vo.req.DynamicTaskQueryVo;
import com.yingxin.yxerp.module.report.task.vo.req.DynamicTaskUpdateVo;
import com.yingxin.yxerp.module.report.task.vo.resp.DynamicTaskRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 动态定时任务管理 前端控制器
 * @author yuebao
 * @since 2023-12-07
 */

@Api(tags = "动态定时任务管理")
@RestController
@RequestMapping("/report/task/dynamic/task")
public class DynamicTaskController {

    @Autowired
    private IDynamicTaskService dynamicTaskService;

    // @PreAuthorize("hasAuthority('dynamic:task:view')")
    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<DynamicTaskRespVo> page(@RequestBody DynamicTaskQueryVo queryVo) {
        return dynamicTaskService.listVo( queryVo );
    }

    @ApiOperation(value = "联机")
    @GetMapping("/online/{id}")
    public void online(@PathVariable("id") String id) {
        dynamicTaskService.online( id );
    }

    // @PreAuthorize("hasAuthority('dynamic:task:view')")
    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public DynamicTaskRespVo get(@PathVariable("id") String id) {
        DynamicTaskRespVo vo = dynamicTaskService.mapper().getVo( id );
        return vo;
    }

    // @PreAuthorize("hasAuthority('dynamic:task:edit')")
    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public void update(@RequestBody DynamicTaskUpdateVo updateVo){
        dynamicTaskService.update( updateVo );
    }

    // @PreAuthorize("hasAuthority('dynamic:task:run')")
    @ApiOperation(value = "任务执行一次")
    @GetMapping("/run/{id}")
    public void run(@PathVariable("id") String id) {
        dynamicTaskService.run( id );
    }

    // @PreAuthorize("hasAuthority('dynamic:task:startOrstop')")
    @ApiOperation(value = "任务启动/停止")
    @PostMapping("/update/status")
    public void updateStatus( @RequestBody DynamicTaskUpdateVo updateVo ) {
        DynamicTask task = dynamicTaskService.getById( updateVo.getId() );
        task.setStatus( updateVo.getStatus() );
        dynamicTaskService.updateStatus( task );
    }

    // @PreAuthorize("hasAuthority('dynamic:task:delete')")
    @ApiOperation(value = "任务删除")
    @GetMapping("/delete/{id}")
    public void delete(@PathVariable("id") String id) {
        dynamicTaskService.delete( id );
    }


    @ApiOperation(value = "来源")
    @PostMapping("/category/list")
    public List<KeyValueRespVo> categoryList() {
        DynamicTaskLogCategoryEnum[] enums = DynamicTaskLogCategoryEnum.values();
        List< KeyValueRespVo > vos = new ArrayList<>();
        for( DynamicTaskLogCategoryEnum e : enums ){
            KeyValueRespVo vo = new KeyValueRespVo();
            vo.setKey( e.getType() );
            vo.setValue( e.getTypeName() );

            vos.add( vo );
        }
        return vos;
    }

    @ApiOperation(value = "任务分类")
    @PostMapping("/type/list")
    public List< KeyValueRespVo > taskTypeList() {
        DynamicTaskTypeEnums[] enums = DynamicTaskTypeEnums.values();
        List< KeyValueRespVo > vos = new ArrayList<>();
        for( DynamicTaskTypeEnums e : enums ){
            KeyValueRespVo vo = new KeyValueRespVo();
            vo.setKey( e.getType() );
            vo.setValue( e.getTypeName() );

            vos.add( vo );
        }
        return vos;
    }

    @ApiOperation(value = "任务名称")
    @PostMapping("/name/list")
    public List< KeyValueRespVo > taskNameList() {
        DynamicTaskName[] enums = DynamicTaskName.values();
        List< KeyValueRespVo > vos = new ArrayList<>();
        for( DynamicTaskName e : enums ){
            KeyValueRespVo vo = new KeyValueRespVo();
            vo.setKey( e.getType() );
            vo.setValue( e.getTypeName() );

            vos.add( vo );
        }
        return vos;
    }

    @ApiOperation(value = "任务执行状态")
    @PostMapping("/status/list")
    public List< KeyValueRespVo > statusList() {
        DynamicTaskLogStatusEnums[] enums = DynamicTaskLogStatusEnums.values();
        List< KeyValueRespVo > vos = new ArrayList<>();
        for( DynamicTaskLogStatusEnums e : enums ){
            KeyValueRespVo vo = new KeyValueRespVo();
            vo.setKey( e.getStatus().toString() );
            vo.setValue( e.getStatusName() );

            vos.add( vo );
        }
        return vos;
    }

}
